package id.co.shuba.spamobile.data;

import id.co.shuba.spamobile.model.StandardInfo;

import java.util.ArrayList;
import java.util.List;

import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

public class DDLDB extends BaseDB {
	
	public DDLDB(DatabaseHandler db) {
		super(db);
	}
	/*
	public void test() {
		
		String sql = "SELECT OPTION_VALUE AS _id, OPTION_TEXT FROM TBL_OPTION WHERE OPTION_TYPE = ?";
		String[] params = new String[] { type };

		SQLiteDatabase db = _db.getReadableDatabase();
		Cursor cursor = db.rawQuery(sql, params);

		List<OptionInfo> list = new ArrayList<OptionInfo>();

		if (cursor.moveToFirst()) {
			do {
				OptionInfo info = new OptionInfo(cursor.getString(0),
						cursor.getString(1));
				list.add(info);
			} while (cursor.moveToNext());
		}

		cursor.close();		
		
		
		sql_cmd = new SQLiteCommand("PRAGMA TABLE_INFO(" + dr["TABLE_NAME"].ToString() + ")");
        DataTable Dt = DBLib.LoadToGridFromReader(sql_cmd, "PRAGMA").Tables[0];
        for (int c = 0; c < Dt.Columns.Count; c++)
        {
            if (Dt.Columns[c].ColumnName.Trim().ToLower() == "name")
            {
                for (int r = 0; r < Dt.Rows.Count; r++)
                {
                    if (Dt.Rows[r][c].ToString().Trim().ToUpper().Replace("[", "").Replace("]", "") == dr["FIELD_NAME"].ToString().Trim().ToUpper().Replace("[", "").Replace("]", ""))
                    {
                        exist = true;
                    }
                }
            }
        }
        if (!exist && dr["IS_TABLE_NEW"].ToString().Trim().ToLower() == "false")
        {
            try
            {
                sql_cmd = new SQLiteCommand("ALTER TABLE " + dr["TABLE_NAME"].ToString() + " ADD COLUMN " + dr["FIELD_NAME"].ToString() + " " + dr["DATA_TYPE"].ToString() + "");
                DBLib.ExecuteNonQuery(sql_cmd);
            }
            catch (Exception) { }
        }
        if (!exist && dr["IS_TABLE_NEW"].ToString().Trim().ToLower() == "true")
        {
            try
            {
                if (Dt.Rows.Count == 0)
                {
                    sql_cmd = new SQLiteCommand(dr["DATA_TYPE"].ToString());
                    DBLib.ExecuteNonQuery(sql_cmd);
                }
            }
            catch (Exception) { }
        }
    }
    catch (Exception)
    {
        try
        {
            if (dr["IS_TABLE_NEW"].ToString().Trim().ToLower() == "true")
            {
                sql_cmd = new SQLiteCommand(dr["DATA_TYPE"].ToString());
                DBLib.ExecuteNonQuery(sql_cmd);
            }
        }
        catch (Exception) { }
    }
	}*/
	
	public void createTable(String sql) {
		SQLiteDatabase db = _db.getWritableDatabase();		
		db.execSQL(sql);
	}
	
	public void alterColumn(String table, String column, String dataType) {
		SQLiteDatabase db = _db.getWritableDatabase();
		String sql = String.format("ALTER TABLE %s ADD COLUMN %s %s", table, column, dataType);
		db.execSQL(sql);
	}
	
	public boolean isColumnExists(String table, String column) {
		String sql = "PRAGMA TABLE_INFO('"+table+"')";		

		SQLiteDatabase db = _db.getReadableDatabase();
		Cursor cursor = db.rawQuery(sql, null);	

		boolean exists = false;
		if (cursor.moveToFirst()) {
			do {
				String c = cursor.getString(cursor.getColumnIndex("name"));
				if (column.toLowerCase().equals(c.toLowerCase())) {
					exists = true;
					break;
				}					
			} while (cursor.moveToNext());
		}

		cursor.close();		
		
		return exists;
	}
}
